// pages/timeCar/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    mainpic: "",
    current: 0,
    num: 0,
    viewId: 'dots0',
    piclist: [
      
    ],
    product: {},
    storeId:0,
    storeName:'',
    isKf:false,
    isAdd:false,
    price:'0',
    typeList:[],
    typeIndex:-1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    wx.hideShareMenu()
    this.setData({
      id: options.id,
      storeId:wx.getStorageSync('userInfo').storeId
    }, () => {
      this.proDetails()
      this.storeProTypeList()
    })
  },
  inputs(e){
    console.log(e.detail.value)
    this.setData({
      price:e.detail.value
    })
  },
  proDetails(){
    let data = {
      id:this.data.id
    }
    app.agriknow.proDetails(data).then(res => {
      if(res.code == '000000'){
        let image = []
        let specifications = []
        if(res.data.image !== null && res.data.image !== "" && res.data.image != []){
          image = res.data.image.split(',');
        }
        this.setData({
          product: {...res.data, image},
          piclist: image,
          price:res.data.price
        })
      }
    })
  },

  handlechange(e){
    this.setData({
      current: e.detail.current,
      viewId: 'dots' + e.detail.current
    })
  },

  handlestrpChange(e){
    this.setData({
      num: e.detail
    })
  },

  handleadd(){
    this.setData({
      num: 1
    })
  },

  handledotclick(e){
    this.setData({
      current: e.currentTarget.dataset.current
    })
  },

  submit(){
    if(this.data.num==0) return
    let data = {
      num:this.data.num,
      packageId:this.data.id,
      price:this.data.num*this.data.product.price,
      remake:this.data.product.price,
      storeId:this.data.storeId,
      userId:wx.getStorageSync('userLogin').userId,
      type:1
    }
    app.agriknow.userBuy(data).then(res=>{
      console.log(res)
      let object = {
        id:res.data,
        price:data.price,
        proList:[
          {
            id:data.packageId,
            num:data.num,
            price:this.data.product.price,
            productImage:this.data.piclist[0],
            productName:this.data.product.name
          }
        ],
        remake:data.remake,
        storeName:this.data.storeName
      }
      wx.navigateTo({
        url: '/pages/submitDetail/index',
        success:function(res){
          res.eventChannel.emit('submitDetail', {id:object.id,object:object,type:1})
        }
      })
    })
  },

  openKf(){
    this.setData({
      isKf:true
    })
  },
  closePopup(){
    this.setData({
      isKf:false
    })
  },
 closePrice(){
  this.setData({
      isAdd:false
    })
},
  openPrice(){
    
    this.setData({
      isAdd:true
    })
  },
  addSure(){
    var product=this.data.product
    var price=this.data.price
    var typeList=this.data.typeList
    var typeIndex=this.data.typeIndex
    if(Number(price)<Number(product.costPrice)){
      wx.showToast({
        title: '对外零售价不能低于供应价！',
        icon:'none',
        duration:1500,
        mask:true
      })
      return false
    }
    if(typeIndex<0){
      wx.showToast({
        title: '请选择添加到哪个产品分类',
        icon:'none',
        duration:1500,
        mask:true
      })
      return false
    }

    let data = {
      id:wx.getStorageSync('userInfo').storeId,
      price:this.data.price,
      productId:this.data.product.id,
      typeId:typeList[typeIndex].id
    }
   
    app.agriknow.storeAddDepotPro(data).then(res=>{
      console.log(res)
      if(res.code=='000000'){
        this.tipsMessage('添加成功')
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1000);
      }else{
        this.tipsMessage(res.data)
      }
    })
  },
  //提示信息
  tipsMessage(text) {
    wx.showToast({
      title: text,
      icon: 'none',
      duration: 2000,
      mask: true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
 
  },

  back(){
    wx.navigateBack({
      delta: 1
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  storeProTypeList(){
    let data = {
      id:wx.getStorageSync('userInfo').storeId
    }
    app.agriknow.storeProTypeList(data).then(res=>{
      console.log(res)
      this.setData({
        typeList:res.data
      })
    })
  },
  pickerChange:function(e){
    var val= e.detail.value
    var key=e.currentTarget.dataset.key+'Index'
    if(val<0){
      return false
    }
    this.setData({
      [key]:val
    })
  },
})